package com.neutech.base;

import com.neutech.constant.Constant;
import com.neutech.frame.TankFrame;
import com.neutech.img.tank.Home;
import com.neutech.terrain.*;
import com.neutech.tank.Bullet;
import com.neutech.tank.EnemyTank;
import com.neutech.tank.Tank;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

import java.awt.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 一张地图，关卡
 */
public class TankMap implements Serializable {
    public Random random = new Random();

    private static final long serialVersionUID = -8604272294784723778L;
    /**
     * 1p我方坦克
     */
    public Tank p1 = null;
    public Tank p2 = null;
    /**
     * home
     */
    public Home home = new Home();

    /**
     * 分数
     */
    public int score = 1;
    public int die = 1;
    public int enemy_die = 0;
    public boolean creat = false;
    int Time=1;
    boolean prop1=true;


    public boolean isProp1() {
        return prop1;
    }

    public void setProp1(boolean prop1) {
        this.prop1 = prop1;
    }

    public int getTime() {
        return Time;
    }

    public void setTime(int time) {
        Time = time;
    }

    //   private String path;
    public void music(String path){
        new Thread(new Runnable() {
            @Override
            public void run() {
                FileInputStream is=null;
                AudioStream as=null;
                try {
                    is=new FileInputStream(path);
                    as=new AudioStream(is);
                    AudioPlayer.player.start(as);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


////    private MapGenerator mapGenerator;
//

//    public int enemy_die =1;
    private Tank tank;/*第一步申明*/

    public int getEnemy_die() {
        return enemy_die;
    }

    public void setEnemy_die(int enemy_die) {
        this.enemy_die = enemy_die;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
    //    public TankMap(Tank tank) {
//        this.tank = tank;
//    }


    //    public TankMap() {
//
//    }
//
//    public TankMap(Tank tank) {
//
//        this.tank = tank;
//    }

    /**
     * 砖墙，铁墙
     */
    public List<Wall> wallList = new CopyOnWriteArrayList<>();
    /**
     * s水的集合
     */
    public List<Water> waterList = new CopyOnWriteArrayList<>();
    /**
     *草的集合
     */
    public List<Grass> grassList = new CopyOnWriteArrayList<>();
    /**
     * 爆炸的集合
     */
    public List<Boom> boomList = new CopyOnWriteArrayList<>();
    /**
     * 道具的集合
     */
    public List<Prop> propList = new CopyOnWriteArrayList<>();

    /**
     * 冰的集合
     */
    public List<Ice> iceList = new CopyOnWriteArrayList<>();
    /**
     * 申明敌军坦克集合
     */
    public List<EnemyTank> enemyTankList = new CopyOnWriteArrayList<>();

    public void gameRunning(Graphics g) {
        /**
         * 设置画笔颜色rgb的值，这是大背景色,这个得卸载背景大小的前面
         */
        g.setColor(new Color(127, 127, 127));
        /**
         * 填充背景色
         */
        g.fillRect(0, 0, Constant.FRAME_WIDTH, Constant.FRAME_HEIGHT);

        /**
         * 设置画笔黑色
         */
        g.setColor(Color.BLACK);
        g.fillRect(Constant.MARGIN_LEFT, Constant.MARGIN_TOP, Constant.BLACK_SIZE, Constant.BLACK_SIZE);
        /**
         * 绘制老家
         */
        home.draw(g);
        /**
         * 绘制所有的冰
         */
        for (Ice ice : iceList) {
            ice.draw(g);
        }


        /**
         * 传画笔过去,绘制一定要放在最后
         */
        /**
         * 去除地图中的p1绘制
         */
        if (p1 != null) {
            p1.draw(g);
        }
        if (p2 != null) {
            p2.draw(g);
        }

//        bullet.draw(g);

        /**
         * 绘制所有的草
         */
        for (Grass grass : grassList) {
            grass.draw(g);
        }

        /**
         * 取出p1的发射子弹绘制
         */
        if (p1 != null) {
            for (Bullet bullet : p1.bulletList) {
                bullet.draw(g);
            }

        }
        if (p2 != null) {
            for (Bullet bullet : p2.bulletList) {
                bullet.draw(g);

            }
        }

        /**
         * 遍历绘制所有的墙
         */
        for (Wall wall : wallList) {
            wall.draw(g);
        }
        for (Water water : waterList) {
            water.draw(g);
        }
        /**
         * 绘制所有的道具
         */
        for (Prop prop : propList) {
            prop.draw(g);
        }



        /**
         * 判断我方子弹与地方坦克是否碰撞
         * 循环嵌套让子弹和所有坦克比较一次，如果他俩的矩形相交就remove掉
         */
        if (p1 != null) {
            for (Bullet bullet : p1.bulletList) {
                for (EnemyTank enemyTank : enemyTankList) {
                    if (bullet.getRectangle().intersects(enemyTank.getRectangle())) {
                        p1.bulletList.remove(bullet);
                        enemyTankList.remove(enemyTank);
                       music("src/com/neutech/music/buh.wav");

                        enemy_die++;

                        setEnemy_die(enemy_die++);
//
                        System.out.println("累计" + score++ + "分数");
                        if (score > 10) {
                            System.out.println("太厉害了！！！");
                            score = 1;
                        }

                        if (getEnemy_die() > 20 && getEnemy_die() < 25) {
                            p1.setBulletsize(20);
//                                g.setFont(new Font("楷体",Font.BOLD,36));
//                                g.drawString("触发隐藏技能",Constant.MARGIN_LEFT+Constant.SPRITE_SIZE*12,Constant.MARGIN_TOP);
                            System.out.println("触发隐藏技能，连发20子弹");
                        }
                        if (getEnemy_die() == 25) {
                            p1.setBulletsize(1);
                        }
                        if (getEnemy_die() < 30) {
                            int randomnum = random.nextInt(3);
                            if (randomnum == 0) {
                                enemyTankList.add(new EnemyTank(Constant.MARGIN_LEFT, Constant.MARGIN_TOP, Constant.SPRITE_SIZE, Constant.SPRITE_SIZE));
                            }
                            if (randomnum == 1) {
                                enemyTankList.add(new EnemyTank(Constant.MARGIN_LEFT + Constant.SPRITE_SIZE * 6, Constant.MARGIN_TOP, Constant.SPRITE_SIZE, Constant.SPRITE_SIZE));
                            }
                            if (randomnum == 2) {
                                enemyTankList.add(new EnemyTank(Constant.MARGIN_LEFT + Constant.SPRITE_SIZE * 12, Constant.MARGIN_TOP, Constant.SPRITE_SIZE, Constant.SPRITE_SIZE));
                            }
                        }
                        boomList.add(new Boom(bullet.getX(), bullet.getY(), Constant.SPRITE_SIZE, Constant.SPRITE_SIZE));//TODO 子弹和墙撞爆炸
                        for (Boom boom : boomList) {
                            boom.draw(g);
                            boomList.remove(boom);
                        }


                    }
                }
            }
        }

        /**
         * 坦克和道具碰撞
         */
        if (p1!=null ){
            Time++;
            if (enemy_die == 1 && Time<=1000) {
                propList.add(new Prop(100,300,Constant.SPRITE_SIZE,Constant.SPRITE_SIZE));
                for (EnemyTank enemyTank : enemyTankList) {
                    for (Prop prop : propList) {//循环遍历每个图片，设置每个图片的下标，所以在这里写setindex//TODO 在这里写换图片的，加上条件
                        prop.setIndex(getEnemy_die());
                        if (prop.getRectangle().intersects(p1.getRectangle()) || Time>=1000){
                            setTime(1000);
                            propList.remove(prop);
                            setProp1(false);
                        }
                    }
                }
            }else {
                for (Prop prop : propList) {
                    propList.remove(prop);
                }

            }
        }


       if (p1!=null ||p2!=null ){
           if (enemy_die == 2 && Time<=2000) {

               propList.add(new Prop(200,200,Constant.SPRITE_SIZE,Constant.SPRITE_SIZE));
               for (EnemyTank enemyTank : enemyTankList) {
                   for (Prop prop : propList) {
                       prop.setIndex(getEnemy_die());
                       if (prop.getRectangle().intersects(p1.getRectangle()) || Time>=2000){
                           setTime(2000);
                           Time++;
                           propList.remove(prop);
                       }
                   }
               }
           }
       }

        if (p1!=null ){
            if (enemy_die == 3 && Time<=3000) {

                propList.add(new Prop(200,300,Constant.SPRITE_SIZE,Constant.SPRITE_SIZE));
                for (EnemyTank enemyTank : enemyTankList) {
                    for (Prop prop : propList) {
                        prop.setIndex(getEnemy_die());
                        if (prop.getRectangle().intersects(p1.getRectangle()) || Time>=3000){
                            setTime(3000);
                            propList.remove(prop);
                        }
                    }
                }
            }
        }

        if (p1!=null){
            if (enemy_die == 4 && Time<=4000) {
                propList.add(new Prop(300,300,Constant.SPRITE_SIZE,Constant.SPRITE_SIZE));
                for (EnemyTank enemyTank : enemyTankList) {
                    for (Prop prop : propList) {
                        prop.setIndex(getEnemy_die());
                        if (prop.getRectangle().intersects(p1.getRectangle()) || Time>=4000){
                            setTime(4000);
                            propList.remove(prop);
                        }
                    }
                }
            }
        }








        new Thread(new Runnable() {//TODO 道具
            @Override
            public void run() {
                for (Prop prop : propList) {
                    if (p1.getRectangle().intersects(prop.getRectangle())){
                        propList.remove(prop);

                    }
                }
            }
        }).start();







        if (p2 != null) {
            for (Bullet bullet : p2.bulletList) {
                for (EnemyTank enemyTank : enemyTankList) {
                    if (bullet.getRectangle().intersects(enemyTank.getRectangle())) {
                        p2.bulletList.remove(bullet);
                        enemyTankList.remove(enemyTank);
                        music("src/com/neutech/music/buh.wav");
                        enemy_die++;
                        setEnemy_die(enemy_die++);
                        System.out.println("累计" + score++ + "分数");
                        if (score > 10) {
                            System.out.println("太厉害了！！！");
                            score = 1;
                        }
                        if (getEnemy_die() > 20 && getEnemy_die() < 25) {
                            p2.setBulletsize(20);
//                                g.setFont(new Font("楷体",Font.BOLD,36));
//                                g.drawString("触发隐藏技能",Constant.MARGIN_LEFT+Constant.SPRITE_SIZE*12,Constant.MARGIN_TOP);
                            System.out.println("触发隐藏技能，连发20子弹");
                        }
                        if (getEnemy_die() == 25) {
                            p2.setBulletsize(1);
                        }
                        if (getEnemy_die() < 30) {
                            int randomnum = random.nextInt(3);
                            if (randomnum == 0) {
                                enemyTankList.add(new EnemyTank(Constant.MARGIN_LEFT, Constant.MARGIN_TOP, Constant.SPRITE_SIZE, Constant.SPRITE_SIZE));
                            }
                            if (randomnum == 1) {
                                enemyTankList.add(new EnemyTank(Constant.MARGIN_LEFT + Constant.SPRITE_SIZE * 6, Constant.MARGIN_TOP, Constant.SPRITE_SIZE, Constant.SPRITE_SIZE));
                            }
                            if (randomnum == 2) {
                                enemyTankList.add(new EnemyTank(Constant.MARGIN_LEFT + Constant.SPRITE_SIZE * 12, Constant.MARGIN_TOP, Constant.SPRITE_SIZE, Constant.SPRITE_SIZE));
                            }
                        }
                        boomList.add(new Boom(bullet.getX(),bullet.getY(),Constant.SPRITE_SIZE,Constant.SPRITE_SIZE));//TODO 子弹和墙撞爆炸
                        for (Boom boom : boomList) {
                            boom.draw(g);
                            boomList.remove(boom);
                        }
                    }
                }
            }
        }

        /**
         * 道具碰撞
         */


        /**
         * 判断我方坦克和敌军子弹碰撞
         */
        if (p1 != null) {
            for (EnemyTank enemyTank : enemyTankList) {
                for (Bullet bullet : enemyTank.bulletList) {
                    if (bullet.getRectangle().intersects(p1.getRectangle())) {
                        System.out.println("游戏结束");
                        score = 1;
                        enemyTank.bulletList.remove(bullet);
                        music("src/com/neutech/music/buh.wav");
                        die++;
                        if (die < 7)
                            p1 = new Tank();

                        if (die ==5) {
                            System.out.println("你太菜了！！！");
                        }
                        if (die > 7)
                        {
                           p1 = null; //TODO 结束画面
                        }
                        boomList.add(new Boom(bullet.getX(),bullet.getY(),Constant.SPRITE_SIZE,Constant.SPRITE_SIZE));//TODO 子弹和墙撞爆炸
                        for (Boom boom : boomList) {
                            boom.draw(g);
                            boomList.remove(boom);
                        }


                    }
                }
            }
        }

        /**
         * 2p
         */
        if (p2 != null) {
            for (EnemyTank enemyTank : enemyTankList) {
                for (Bullet bullet : enemyTank.bulletList) {
                    if (bullet.getRectangle().intersects(p2.getRectangle())) {
                        System.out.println("游戏结束");
                        score = 1;
                        enemyTank.bulletList.remove(bullet);
                        music("src/com/neutech/music/buh.wav");
                        die++;
                        if (die < 7)
                            p2 = new Tank();

                        if (die ==5) {
                            System.out.println("你太菜了！！！");
                        }
                        if (die > 7)
                        {
                            p2 = null; //TODO 结束画面
                        }
                        boomList.add(new Boom(bullet.getX(),bullet.getY(),Constant.SPRITE_SIZE,Constant.SPRITE_SIZE));//TODO 子弹和墙撞爆炸
                        for (Boom boom : boomList) {
                            boom.draw(g);
                            boomList.remove(boom);
                        }


                    }
                }
            }
        }


        /**
         * 2p
         */


        /**
         * 判断敌方坦克子弹和墙的碰撞
         */
        for (EnemyTank enemyTank : enemyTankList) {
            for (Bullet bullet : enemyTank.bulletList) {
                for (Wall wall : wallList) {
                    if (wall.getRectangle().intersects(bullet.getRectangle()) && wall.wallType == 1) {
                        wallList.remove(wall);
                        enemyTank.bulletList.remove(bullet);
                        music("src/com/neutech/music/buh.wav");
                        boomList.add(new Boom(wall.getX(),wall.getY(),Constant.WALL_SIZE,Constant.WALL_SIZE));//TODO 子弹和墙撞爆炸
                        for (Boom boom : boomList) {
                            boom.draw(g);
                            boomList.remove(boom);
                        }
                    }
                    if (wall.getRectangle().intersects(bullet.getRectangle()) && wall.wallType == 2) {
                        enemyTank.bulletList.remove(bullet);
                        boomList.add(new Boom(wall.getX(),wall.getY(),Constant.WALL_SIZE,Constant.WALL_SIZE));//TODO 子弹和墙撞爆炸
                        for (Boom boom : boomList) {
                            boom.draw(g);
                            boomList.remove(boom);
                        }
                    }
                }
            }
        }
        /**
         * 我方子弹和墙
         */
        if (p1 != null) {
            for (Bullet bullet : p1.bulletList) {
                for (Wall wall : wallList) {
                    if (wall.getRectangle().intersects(bullet.getRectangle()) && wall.wallType == 1) {
                        boomList.add(new Boom(wall.getX(),wall.getY(),Constant.WALL_SIZE,Constant.WALL_SIZE));//TODO 子弹和墙撞爆炸
                        for (Boom boom : boomList) {
                            boom.draw(g);
                            boomList.remove(boom);
                        }
                        wallList.remove(wall);
                        p1.bulletList.remove(bullet);
//                        music("src/com/neutech/music/buh.wav");
                    }
                    if (wall.getRectangle().intersects(bullet.getRectangle()) && wall.wallType == 2) {
                        p1.bulletList.remove(bullet);
                        boomList.add(new Boom(wall.getX(),wall.getY(),34,34));//TODO 子弹和墙撞爆炸
                        for (Boom boom : boomList) {
                            boom.draw(g);
                            boomList.remove(boom);
                        }
                    }
                }
            }
        }

        if (p2 != null) {
            for (Bullet bullet : p2.bulletList) {
                for (Wall wall : wallList) {
                    if (wall.getRectangle().intersects(bullet.getRectangle()) && wall.wallType == 1) {
                        wallList.remove(wall);
                        p2.bulletList.remove(bullet);
                        music("src/com/neutech/music/buh.wav");
                        boomList.add(new Boom(wall.getX(),wall.getY(),34,34));//TODO 子弹和墙撞爆炸
                        for (Boom boom : boomList) {
                            boom.draw(g);
                            boomList.remove(boom);
                        }
                    }
                    if (wall.getRectangle().intersects(bullet.getRectangle()) && wall.wallType == 2) {
                        p2.bulletList.remove(bullet);
                        boomList.add(new Boom(wall.getX(),wall.getY(),34,34));//TODO 子弹和墙撞爆炸
                        for (Boom boom : boomList) {
                            boom.draw(g);
                            boomList.remove(boom);
                        }
                    }
                }
            }
        }


        /**
         * 敌方坦克和水撞
         */
        for (Water water : waterList) {
            for (EnemyTank enemyTank : enemyTankList) {
                if (water.getRectangle().intersects(enemyTank.getRectangle())) {
                    if (enemyTank.direction == Direction.UP) {
                        enemyTank.setY(water.getY() + Constant.SPRITE_SIZE - Constant.WALL_SIZE);
                    }
                    if (enemyTank.direction == Direction.RIGHT) {
                        enemyTank.setX(water.getX() - Constant.SPRITE_SIZE);
                    }
                    if (enemyTank.direction == Direction.DOWN) {
                        enemyTank.setY(water.getY() - Constant.SPRITE_SIZE);
                    }
                    if (enemyTank.direction == Direction.LEFT) {
                        enemyTank.setX(water.getX() + Constant.SPRITE_SIZE - Constant.WALL_SIZE);
                    }
                }
            }
        }

        /**
         * 我方坦克和水撞1p
         */

        if (p1 != null && prop1==true) {
        for (Water water : waterList) {
            for (EnemyTank enemyTank : enemyTankList) {
                if (water.getRectangle().intersects(p1.getRectangle())) {
                    if (p1.direction == Direction.UP) {
                        p1.setY(water.getY() + Constant.SPRITE_SIZE);
                    }
                    if (p1.direction == Direction.RIGHT) {
                        p1.setX(water.getX() - Constant.SPRITE_SIZE);

                    }
                    if (p1.direction == Direction.DOWN) {
                        p1.setY(water.getY() - Constant.SPRITE_SIZE);

                    }
                    if (p1.direction == Direction.LEFT) {
                        p1.setX(water.getX() + Constant.SPRITE_SIZE);

                    }
                }
            }
        }
    }
        /**
         * 2p
         */
        if (p2 != null) {
        for (Water water : waterList) {
            for (EnemyTank enemyTank : enemyTankList) {
                if (water.getRectangle().intersects(p2.getRectangle())) {
                    if (p2.direction == Direction.UP) {
                        p2.setY(water.getY() + Constant.SPRITE_SIZE - Constant.WALL_SIZE);
                    }
                    if (p2.direction == Direction.RIGHT) {
                        p2.setX(water.getX() - Constant.SPRITE_SIZE);
                    }
                    if (p2.direction == Direction.DOWN) {
                        p2.setY(water.getY() - Constant.SPRITE_SIZE);
                    }
                    if (p2.direction == Direction.LEFT) {
                        p2.setX(water.getX() + Constant.SPRITE_SIZE - Constant.WALL_SIZE);
                    }
                }
            }
        }
    }

        /**
         * 我方坦克走到冰上
         */
        if (p1 != null){
        for (Ice ice : iceList) {
            for (EnemyTank enemyTank : enemyTankList) {
                if (ice.getRectangle().intersects(p1.getRectangle())){
//                   tank.setSpeed(15);
                    p1.setSpeed(20);
                }
            }
        }
    }


        /**
         * 判断敌方坦克撞墙
         */
        for (Wall wall : wallList) {
            for (EnemyTank enemyTank : enemyTankList) {
                if (wall.getRectangle().intersects(enemyTank.getRectangle())){
                    if (enemyTank.getRectangle().intersects(wall.getRectangle()) && enemyTank.direction == Direction.UP)
                    {
                        enemyTank.setY(wall.getY() + Constant.SPRITE_SIZE - Constant.WALL_SIZE);
                    }
                    if (enemyTank.getRectangle().intersects(wall.getRectangle()) && enemyTank.direction == Direction.RIGHT)
                    {
                        enemyTank.setX(wall.getX() - Constant.SPRITE_SIZE);
                    }
                    if (enemyTank.getRectangle().intersects(wall.getRectangle()) && enemyTank.direction == Direction.DOWN)
                    {
                        enemyTank.setY(wall.getY() - Constant.SPRITE_SIZE);
                    }
                    if (enemyTank.getRectangle().intersects(wall.getRectangle()) && enemyTank.direction == Direction.LEFT)
                    {
                        enemyTank.setX(wall.getX() + Constant.SPRITE_SIZE-Constant.WALL_SIZE);
                    }
                }
            }
        }
        /**
         * 判断我方坦克撞墙
         */
        if (p1 != null){
            for (Wall wall : wallList) {

                if (p1.getRectangle().intersects(wall.getRectangle()) && p1.direction == Direction.UP)
                {
                    p1.setY(wall.getY() + Constant.SPRITE_SIZE - Constant.WALL_SIZE);
                }
                if (p1.getRectangle().intersects(wall.getRectangle()) && p1.direction == Direction.RIGHT)
                {
                    p1.setX(wall.getX() - Constant.SPRITE_SIZE);
                }
                if (p1.getRectangle().intersects(wall.getRectangle()) && p1.direction == Direction.DOWN)
                {
                    p1.setY(wall.getY() - Constant.SPRITE_SIZE);
                }
                if (p1.getRectangle().intersects(wall.getRectangle()) && p1.direction == Direction.LEFT)
                {
                    p1.setX(wall.getX() + Constant.SPRITE_SIZE-Constant.WALL_SIZE);
                }
            }
        }
        if (p2 != null){
            for (Wall wall : wallList) {

                if (p2.getRectangle().intersects(wall.getRectangle()) && p2.direction == Direction.UP)
                {
                    p2.setY(wall.getY() + Constant.SPRITE_SIZE -Constant.WALL_SIZE);
                }
                if (p2.getRectangle().intersects(wall.getRectangle()) && p2.direction == Direction.RIGHT)
                {
                    p2.setY(wall.getY() + Constant.SPRITE_SIZE);
                }
                if (p2.getRectangle().intersects(wall.getRectangle()) && p2.direction == Direction.DOWN)
                {
                    p2.setY(wall.getY() - Constant.SPRITE_SIZE);
                }
                if (p2.getRectangle().intersects(wall.getRectangle()) && p2.direction == Direction.LEFT)
                {
                    p2.setX(wall.getX() + Constant.SPRITE_SIZE-Constant.WALL_SIZE);
                }
            }
        }

        /**
         * 我方坦克子弹和老家碰撞的判断
         */
        //p1
        if (p1 != null){
        for (Bullet bullet : p1.bulletList) {
            if (bullet.getRectangle().intersects(home.getRectangle())){
                home.setLive(false);
                music("src/com/neutech/music/buh.wav");
                boomList.add(new Boom(home.getX(),home.getY()+Constant.WALL_SIZE,Constant.SPRITE_SIZE,Constant.SPRITE_SIZE));//TODO 子弹和墙撞爆炸
                for (Boom boom : boomList) {
                    boom.draw(g);
                    boomList.remove(boom);
                }
            }
        } }

        //p2
        if (p2 != null){
            for (Bullet bullet : p2.bulletList) {
                if (bullet.getRectangle().intersects(home.getRectangle())){
                    home.setLive(false);
                    music("src/com/neutech/music/buh.wav");
                    boomList.add(new Boom(home.getX(),home.getY(),Constant.SPRITE_SIZE,Constant.SPRITE_SIZE));//TODO 子弹和墙撞爆炸
                    for (Boom boom : boomList) {
                        boom.draw(g);
                        boomList.remove(boom);
                    }
                }
            } }
        /**
         * 敌军子弹和老家的判断
         */
        for (EnemyTank enemyTank : enemyTankList) {
            for (Bullet bullet : enemyTank.bulletList) {
                if (bullet.getRectangle().intersects(home.getRectangle())){
                    home.setLive(false);
                    music("src/com/neutech/music/buh.wav");
                    boomList.add(new Boom(home.getX(),home.getY(),Constant.SPRITE_SIZE,Constant.SPRITE_SIZE));//TODO 子弹和墙撞爆炸
                    for (Boom boom : boomList) {
                        boom.draw(g);
                        boomList.remove(boom);
                    }
                }
            }
        }

/**
 * 老家死
 */







        /**
         * 判断我方坦克子弹和敌军坦克子弹碰撞
         */
        if (p1 != null){
            for (Bullet bullet : p1.bulletList) {
                for (EnemyTank enemyTank : enemyTankList) {
                    for (Bullet bullet1 : enemyTank.bulletList) {
                        if (bullet.getRectangle().intersects(bullet1.getRectangle())){
                            enemyTank.bulletList.remove(bullet1);
                            p1.bulletList.remove(bullet);
//                            music("src/com/neutech/music/bu.wav");
                            boomList.add(new Boom(bullet.getX(),bullet.getY(),Constant.WALL_SIZE,Constant.WALL_SIZE));//TODO 子弹和墙撞爆炸
                            for (Boom boom : boomList) {
                                boom.draw(g);
                                boomList.remove(boom);
                            }
                        }
                    }
                }
            }
        }

        if (p2 != null){
            for (Bullet bullet : p2.bulletList) {
                for (EnemyTank enemyTank : enemyTankList) {
                    for (Bullet bullet1 : enemyTank.bulletList) {
                        if (bullet.getRectangle().intersects(bullet1.getRectangle())){
                            enemyTank.bulletList.remove(bullet1);
                            p2.bulletList.remove(bullet);
                            music("src/com/neutech/music/bu.wav");
                            boomList.add(new Boom(bullet.getX(),bullet.getY(),Constant.WALL_SIZE,Constant.WALL_SIZE));//TODO 子弹和墙撞爆炸
                            for (Boom boom : boomList) {
                                boom.draw(g);
                                boomList.remove(boom);
                            }
                        }
                    }
                }
            }
        }



        /**
         * 判断我方坦克和敌军坦克相撞
         */
        if (p1 != null){
        for (EnemyTank enemyTank : enemyTankList) {
            if (p1.getRectangle().intersects(enemyTank.getRectangle()) && p1.direction == Direction.UP ) {
                enemyTank.move1 = false;
                p1.setY(enemyTank.getY() + Constant.SPRITE_SIZE - Constant.WALL_SIZE);
                enemyTank.setY(p1.getY() - Constant.SPRITE_SIZE);

            }

            if (p1.getRectangle().intersects(enemyTank.getRectangle()) && p1.direction == Direction.RIGHT) {
                enemyTank.move1 = false;
                p1.setX(enemyTank.getX() - Constant.SPRITE_SIZE);
                enemyTank.setX(p1.getX() + Constant.SPRITE_SIZE);

            }
            if (p1.getRectangle().intersects(enemyTank.getRectangle()) && p1.direction == Direction.DOWN) {
                enemyTank.move1 = false;
                p1.setY(enemyTank.getY() - Constant.SPRITE_SIZE);
                enemyTank.setY(p1.getY() + Constant.SPRITE_SIZE);


            }
            if (p1.getRectangle().intersects(enemyTank.getRectangle()) && p1.direction == Direction.LEFT) {
                enemyTank.move1 = false;
                p1.setX(enemyTank.getX() + Constant.SPRITE_SIZE - Constant.WALL_SIZE);
                enemyTank.setX(p1.getX()-Constant.SPRITE_SIZE);


            }
        }
            }

        /**
         * p2我方坦克和敌方坦克相撞
         */

        if (p2 != null){
            for (EnemyTank enemyTank : enemyTankList) {
                if (p2.getRectangle().intersects(enemyTank.getRectangle()) && p2.direction == Direction.UP) {
                    p2.setY(enemyTank.getY() + Constant.SPRITE_SIZE - Constant.WALL_SIZE);
                }
                if (p2.getRectangle().intersects(enemyTank.getRectangle()) && p2.direction == Direction.RIGHT) {
                    p2.setX(enemyTank.getX() - Constant.SPRITE_SIZE);
                }
                if (p2.getRectangle().intersects(enemyTank.getRectangle()) && p2.direction == Direction.DOWN) {
                    p2.setY(enemyTank.getY() - Constant.SPRITE_SIZE);
                }
                if (p2.getRectangle().intersects(enemyTank.getRectangle()) && p2.direction == Direction.LEFT) {
                    p2.setX(enemyTank.getX() + Constant.SPRITE_SIZE - Constant.WALL_SIZE);
                }
            }
        }

        /**
         * p1和p2互相打
         */
           if (p1!=null && p2!=null){
               for (EnemyTank enemyTank : enemyTankList) {
                   for (Bullet bullet : p1.bulletList) {
                       if (bullet.getRectangle().intersects(p2.getRectangle())){
                           p2 = null;
                           //TODO p1干掉p2
                           p1.bulletList.remove(bullet);
                           p1.setBulletsize(20);
                           enemyTank.setSpeed(10);
                           music("src/com/neutech/music/buh.wav");
                           boomList.add(new Boom(bullet.getX(),bullet.getY(),Constant.SPRITE_SIZE,Constant.SPRITE_SIZE));//TODO 子弹和墙撞爆炸
                           for (Boom boom : boomList) {
                               boom.draw(g);
                               boomList.remove(boom);
                           }
                       }
                   }
               }
           }
        //TODO p2干掉p1
        if (p1!=null && p2!=null){
            for (EnemyTank enemyTank : enemyTankList) {
                for (Bullet bullet : p2.bulletList) {
                    if (bullet.getRectangle().intersects(p1.getRectangle())){
                        p1 = null;
                        //TODO p1干掉p2
                        p2.bulletList.remove(bullet);
                        p2.setBulletsize(20);
                        enemyTank.setSpeed(10);
                        music("src/com/neutech/music/buh.wav");
                        boomList.add(new Boom(bullet.getX(),bullet.getY(),Constant.SPRITE_SIZE,Constant.SPRITE_SIZE));//TODO 子弹和墙撞爆炸
                        for (Boom boom : boomList) {
                            boom.draw(g);
                            boomList.remove(boom);
                        }
                    }
                }
            }
        }
       //TODO 子弹打到边框

//        for (EnemyTank enemyTank : enemyTankList) {
//            for (Bullet bullet : enemyTank.bulletList) {
//                if (bullet.getX()<Constant.MARGIN_LEFT){
//                    boomList.add(new Boom(bullet.getX(),bullet.getY(),Constant.WALL_SIZE,Constant.WALL_SIZE));//TODO 子弹和墙撞爆炸
//                    for (Boom boom : boomList) {
//                        boom.draw(g);
//                        boomList.remove(boom);
//                    }
//                }
//                if (bullet.getX()>Constant.MARGIN_LEFT + Constant.BLACK_SIZE){
//                    boomList.add(new Boom(bullet.getX(),bullet.getY(),Constant.WALL_SIZE,Constant.WALL_SIZE));//TODO 子弹和墙撞爆炸
//                    for (Boom boom : boomList) {
//                        boom.draw(g);
//                        boomList.remove(boom);
//                    }
//                }
//                if (bullet.getY()>Constant.MARGIN_TOP){
//                    boomList.add(new Boom(bullet.getX(),bullet.getY(),Constant.WALL_SIZE,Constant.WALL_SIZE));//TODO 子弹和墙撞爆炸
//                    for (Boom boom : boomList) {
//                        boom.draw(g);
//                        boomList.remove(boom);
//                    }
//                }
//                if (bullet.getY()>Constant.MARGIN_TOP + Constant.BLACK_SIZE){
//                    boomList.add(new Boom(bullet.getX(),bullet.getY(),Constant.WALL_SIZE,Constant.WALL_SIZE));//TODO 子弹和墙撞爆炸
//                    for (Boom boom : boomList) {
//                        boom.draw(g);
//                        boomList.remove(boom);
//                    }
//                }
//            }
//        }








        /**
         * 取出地图中敌军绘制
         */
        for (EnemyTank enemyTank : enemyTankList) {
            enemyTank.draw(g);
            for (Bullet bullet : enemyTank.bulletList) {
                bullet.draw(g);
            }
        }

        /**
         * 绘制文字
         */
       g.setFont(new Font("楷体",Font.BOLD,28));
       g.drawString("坦克",450,100);

       g.setFont(new Font("楷体",Font.BOLD,16));
       g.drawString("20",450,150);



    }



}
